package cc.wfu.types.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 邮箱工具类
 * @Date: 2024-05-16-13:01
 */
@Slf4j
@Component
public class EmailService {

    @Value("${mail.username}")
    private String mailFrom;

    @Resource
    private JavaMailSender javaMailSender;

    /**
     * 发送邮件
     * @param title 邮件标题
     * @param content 邮件内容
     * @param email 目的邮件
     */
    public void sendMail(String title, String content, String email) {
        try{
            MimeMessage message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(mailFrom); // 发送人的邮箱
            helper.setSubject(title); //标题
            helper.setTo(email); //发给谁  对方邮箱
            helper.setText(content, false);
            message.setContent(content, "text/html;charset=UTF-8");
            log.info("邮件验证码 - 发送人: {}, 收件人: {}. 发送成功!", mailFrom, email);
        } catch (Exception e){
            log.error("邮件发送失败!", e);
        }
    }


}
